<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>Huffman.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="Huffman code in Java">
<meta NAME="TITLE" CONTENT="Huffman code in Java">
<meta NAME="KEYWORDS" CONTENT="Huffman,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>Huffman.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "Huffman.java">Huffman.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac Huffman.java</span>
<span class="comment"> *  Execution:    java Huffman - &lt; input.txt   (compress)</span>
<span class="comment"> *  Execution:    java Huffman + &lt; input.txt   (expand)</span>
<span class="comment"> *  Dependencies: BinaryIn.java BinaryOut.java</span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/55compression/abra.txt</span>
<span class="comment"> *                </span><span class="url">http://algs4.cs.princeton.edu/55compression/tinytinyTale.txt</span>
<span class="comment"> *</span>
<span class="comment"> *  Compress or expand a binary input stream using the Huffman algorithm.</span>
<span class="comment"> *</span>
<span class="comment"> *  % java Huffman - &lt; abra.txt | java BinaryDump 60</span>
<span class="comment"> *  010100000100101000100010010000110100001101010100101010000100</span>
<span class="comment"> *  000000000000000000000000000110001111100101101000111110010100</span>
<span class="comment"> *  120 bits</span>
<span class="comment"> *</span>
<span class="comment"> *  % java Huffman - &lt; abra.txt | java Huffman +</span>
<span class="comment"> *  ABRACADABRA!</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Huffman</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class provides static methods for compressing</span>
<span class="comment"> *  and expanding a binary input using Huffman codes over the 8-bit extended</span>
<span class="comment"> *  ASCII alphabet.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/55compress"</span><span class="keyword">&gt;</span><span class="comment">Section 5.5</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Huffman</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="comment">// alphabet size of extended ASCII</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> R </span><span class="symbol">=</span><span class="normal"> </span><span class="number">256</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// Do not instantiate.</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="function">Huffman</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// Huffman trie node</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Node</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> Comparable</span><span class="symbol">&lt;</span><span class="normal">Node</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">char</span><span class="normal"> ch</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> freq</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> left</span><span class="symbol">,</span><span class="normal"> right</span><span class="symbol">;</span>

<span class="normal">        </span><span class="function">Node</span><span class="symbol">(</span><span class="type">char</span><span class="normal"> ch</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> freq</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> left</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> right</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">ch    </span><span class="symbol">=</span><span class="normal"> ch</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">freq  </span><span class="symbol">=</span><span class="normal"> freq</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">left  </span><span class="symbol">=</span><span class="normal"> left</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> right</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// is the node a leaf node?</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isLeaf</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">assert</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">left </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">right </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">))</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">left </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">right </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">));</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">left </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">right </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// compare, based on frequency</span>
<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">compareTo</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">freq </span><span class="symbol">-</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">freq</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads a sequence of 8-bit bytes from standard input; compresses them</span>
<span class="comment">     * using Huffman codes with an 8-bit alphabet; and writes the results</span>
<span class="comment">     * to standard output.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">compress</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="comment">// read the input</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> BinaryStdIn</span><span class="symbol">.</span><span class="function">readString</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">char</span><span class="symbol">[]</span><span class="normal"> input </span><span class="symbol">=</span><span class="normal"> s</span><span class="symbol">.</span><span class="function">toCharArray</span><span class="symbol">();</span>

<span class="normal">        </span><span class="comment">// tabulate frequency counts</span>
<span class="normal">        </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> freq </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">R</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> input</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            freq</span><span class="symbol">[</span><span class="normal">input</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]]++;</span>

<span class="normal">        </span><span class="comment">// build Huffman trie</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> root </span><span class="symbol">=</span><span class="normal"> </span><span class="function">buildTrie</span><span class="symbol">(</span><span class="normal">freq</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// build code table</span>
<span class="normal">        String</span><span class="symbol">[]</span><span class="normal"> st </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> String</span><span class="symbol">[</span><span class="normal">R</span><span class="symbol">];</span>
<span class="normal">        </span><span class="function">buildCode</span><span class="symbol">(</span><span class="normal">st</span><span class="symbol">,</span><span class="normal"> root</span><span class="symbol">,</span><span class="normal"> </span><span class="string">""</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// print trie for decoder</span>
<span class="normal">        </span><span class="function">writeTrie</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// print number of bytes in original uncompressed message</span>
<span class="normal">        BinaryStdOut</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="normal">input</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// use Huffman code to encode input</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> input</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">String</span><span class="normal"> code </span><span class="symbol">=</span><span class="normal"> st</span><span class="symbol">[</span><span class="normal">input</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]];</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> code</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">();</span><span class="normal"> j</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">code</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">j</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'0'</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    BinaryStdOut</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="keyword">false</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">code</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">j</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'1'</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    BinaryStdOut</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="keyword">true</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalStateException</span><span class="symbol">(</span><span class="string">"Illegal state"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// close output stream</span>
<span class="normal">        BinaryStdOut</span><span class="symbol">.</span><span class="function">close</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// build the Huffman trie given frequencies</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">buildTrie</span><span class="symbol">(</span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> freq</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// initialze priority queue with singleton trees</span>
<span class="normal">        </span><span class="usertype">MinPQ&lt;Node&gt;</span><span class="normal"> pq </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> MinPQ</span><span class="symbol">&lt;</span><span class="normal">Node</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">char</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> R</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">freq</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">                pq</span><span class="symbol">.</span><span class="function">insert</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">,</span><span class="normal"> freq</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">],</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">));</span>

<span class="normal">        </span><span class="comment">// special case in case there is only one character with a nonzero frequency</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">pq</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">freq</span><span class="symbol">[</span><span class="string">'</span><span class="specialchar">\0</span><span class="string">'</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> pq</span><span class="symbol">.</span><span class="function">insert</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="string">'</span><span class="specialchar">\0</span><span class="string">'</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">));</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal">                 pq</span><span class="symbol">.</span><span class="function">insert</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="string">'</span><span class="specialchar">\1</span><span class="string">'</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">));</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// merge two smallest trees</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">pq</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">Node</span><span class="normal"> left  </span><span class="symbol">=</span><span class="normal"> pq</span><span class="symbol">.</span><span class="function">delMin</span><span class="symbol">();</span>
<span class="normal">            </span><span class="usertype">Node</span><span class="normal"> right </span><span class="symbol">=</span><span class="normal"> pq</span><span class="symbol">.</span><span class="function">delMin</span><span class="symbol">();</span>
<span class="normal">            </span><span class="usertype">Node</span><span class="normal"> parent </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="string">'</span><span class="specialchar">\0</span><span class="string">'</span><span class="symbol">,</span><span class="normal"> left</span><span class="symbol">.</span><span class="normal">freq </span><span class="symbol">+</span><span class="normal"> right</span><span class="symbol">.</span><span class="normal">freq</span><span class="symbol">,</span><span class="normal"> left</span><span class="symbol">,</span><span class="normal"> right</span><span class="symbol">);</span>
<span class="normal">            pq</span><span class="symbol">.</span><span class="function">insert</span><span class="symbol">(</span><span class="normal">parent</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> pq</span><span class="symbol">.</span><span class="function">delMin</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">// write bitstring-encoded trie to standard output</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">writeTrie</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="function">isLeaf</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            BinaryStdOut</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="keyword">true</span><span class="symbol">);</span>
<span class="normal">            BinaryStdOut</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">ch</span><span class="symbol">,</span><span class="normal"> </span><span class="number">8</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">return</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        BinaryStdOut</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="keyword">false</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">writeTrie</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">writeTrie</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// make a lookup table from symbols and their encodings</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">buildCode</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> st</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">x</span><span class="symbol">.</span><span class="function">isLeaf</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="function">buildCode</span><span class="symbol">(</span><span class="normal">st</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal">  s </span><span class="symbol">+</span><span class="normal"> </span><span class="string">'0'</span><span class="symbol">);</span>
<span class="normal">            </span><span class="function">buildCode</span><span class="symbol">(</span><span class="normal">st</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> s </span><span class="symbol">+</span><span class="normal"> </span><span class="string">'1'</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            st</span><span class="symbol">[</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">ch</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> s</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Reads a sequence of bits that represents a Huffman-compressed message from</span>
<span class="comment">     * standard input; expands them; and writes the results to standard output.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">expand</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// read in Huffman trie from input stream</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> root </span><span class="symbol">=</span><span class="normal"> </span><span class="function">readTrie</span><span class="symbol">();</span><span class="normal"> </span>

<span class="normal">        </span><span class="comment">// number of bytes to write</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> length </span><span class="symbol">=</span><span class="normal"> BinaryStdIn</span><span class="symbol">.</span><span class="function">readInt</span><span class="symbol">();</span>

<span class="normal">        </span><span class="comment">// decode using the Huffman trie</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> length</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">Node</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> root</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">x</span><span class="symbol">.</span><span class="function">isLeaf</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">boolean</span><span class="normal"> bit </span><span class="symbol">=</span><span class="normal"> BinaryStdIn</span><span class="symbol">.</span><span class="function">readBoolean</span><span class="symbol">();</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">bit</span><span class="symbol">)</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal">     x </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            BinaryStdOut</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">ch</span><span class="symbol">,</span><span class="normal"> </span><span class="number">8</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        BinaryStdOut</span><span class="symbol">.</span><span class="function">close</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">readTrie</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">boolean</span><span class="normal"> isLeaf </span><span class="symbol">=</span><span class="normal"> BinaryStdIn</span><span class="symbol">.</span><span class="function">readBoolean</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">isLeaf</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="normal">BinaryStdIn</span><span class="symbol">.</span><span class="function">readChar</span><span class="symbol">(),</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="string">'</span><span class="specialchar">\0</span><span class="string">'</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="function">readTrie</span><span class="symbol">(),</span><span class="normal"> </span><span class="function">readTrie</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sample client that calls </span><span class="keyword">&lt;tt&gt;</span><span class="comment">compress()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if the command-line</span>
<span class="comment">     * argument is "-" an </span><span class="keyword">&lt;tt&gt;</span><span class="comment">expand()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if it is "+".</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal">      </span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"-"</span><span class="symbol">))</span><span class="normal"> </span><span class="function">compress</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">].</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"+"</span><span class="symbol">))</span><span class="normal"> </span><span class="function">expand</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Illegal command line argument"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
